/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 23:14:00
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.edu.bean.EduRandomSelectQuestionBean;
import com.joysuch.wwyt.edu.bean.EduRandomSelectQuestionConfig;
import com.joysuch.wwyt.edu.bean.EduTestPaperEditBean;
import com.joysuch.wwyt.edu.bean.EduTestPaperListBean;
import com.joysuch.wwyt.edu.bean.EduTestPaperListBean.SearchEduTestPaper;
import com.joysuch.wwyt.edu.entity.EduTestPaper;
import com.joysuch.wwyt.edu.repository.EduExamTestPaperDao;
import com.joysuch.wwyt.edu.service.EduExamRecordService;
import com.joysuch.wwyt.edu.service.EduExamUserAnswerService;
import com.joysuch.wwyt.edu.service.EduTestPaperQuestionService;
import com.joysuch.wwyt.edu.service.EduTestPaperService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "教育-试卷管理")
@RequestMapping("/edu/test-paper")
public class EduTestPaperController {

    private static final Logger logger = LoggerFactory.getLogger(EduTestPaperController.class);

    @Autowired
    private EduTestPaperService eduTestPaperService;
    @Autowired
    private EduExamTestPaperDao eduExamTestPaperDao;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("edu:test:paper:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduTestPaperEditBean entity) {
        eduTestPaperService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:test:paper:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduTestPaperEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduTestPaperService.update(entity);
        return ResultBean.success("");
    }

    @RequestMapping("random-select")
    public Object randomSelect(Long testPaperId, String config) {
        List<EduRandomSelectQuestionConfig> configs = new Gson().fromJson(config,
                new TypeToken<List<EduRandomSelectQuestionConfig>>() {
                }.getType());
        eduTestPaperService.validateConfigRules(configs);
        List<EduRandomSelectQuestionBean> list = eduTestPaperService.randomSelectQuestions(testPaperId, configs);
        return ResultBean.success(list);
    }

    // @RequiresPermissions("edu:test:paper")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduTestPaper condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduTestPaperListBean> data = eduTestPaperService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    // @RequiresPermissions("edu:test:paper")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduTestPaper condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduTestPaperListBean> data = eduTestPaperService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    //web端试卷查询
    @RequestMapping("web-page")
    public Object webPage(Pageable page,SearchEduTestPaper condition){
        page = PageDataUtils.addOrderByDesc(page,"id");
        Page<EduTestPaperListBean> data = eduTestPaperService.findByWebPage(page,condition);
        return ResultBean.pageData(data.getContent(),data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(EduTestPaper.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("edu:test:paper:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        int num = eduExamTestPaperDao.countByPaperId(id);
        if (num > 0) {
            throw new IllegalArgumentException("该试卷已关联考试，不能删除");
        }
        eduTestPaperService.delete(id);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:test:paper")
    @RequestMapping("get-question-list")
    public Object getQuestionList(@Valid @NotNull(field = "id") Long id, @RequestParam(value = "userId", required = false) Long userId) {
        return ResultBean.success(eduTestPaperService.getQuestionList(id, userId));
    }

    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id, @RequestParam(value = "userId", required = false) Long userId) {
        return ResultBean.success(eduTestPaperService.getById(id, userId));
    }

    @RequestMapping("paper-excel")
    public void paperExcel(@Valid @NotNull(field = "id")Long id,Integer type,HttpServletResponse response){ eduTestPaperService.paperExcel(id,type,response);
    }
}
